package ysq.xyg.web.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.web.dao.WebAccountDao;
import ysq.xyg.web.model.po.WebAccount;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.web.model.vo.WebAccountVo;
import ysq.xyg.web.service.WebAccountService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * 业务模块-微信账号service实现
 *
 * @author shengQiang yu
 * @date 2019-4-26 11:34:01
 */
@Service("webAccountService")
public class WebAccountServiceImpl extends BaseServiceImpl<WebAccount> implements WebAccountService {

    @Resource
    private WebAccountDao webAccountDao;

    private WebAccount webAccount;

    @Autowired
    public void setBaseDao(WebAccountDao webAccountDao) {
        this.baseDao = webAccountDao;
    }


    /**
     * 分页查询
     *
     * @param webAccountVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-4-26 11:34:01
     */
    public ReturnEntity page(WebAccountVo webAccountVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<WebAccount> list = acceptList(webAccountVo);
        PageInfo<WebAccount> page=new PageInfo<WebAccount>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param webAccountVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-4-26 11:34:01
     */
    public ReturnEntity addOrUpdate(WebAccountVo webAccountVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(webAccountVo.getId())){
            webAccount=new WebAccount(currentUser);
            isUpate=false;
        }else{
            webAccount=selectByPrimaryKey(webAccountVo.getId());
            if(ObjectUtil.isNull(webAccount)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            webAccount.setUpdateTime(new Date());
            webAccount.setUpdateUser(currentUser.getId());
        }
        webAccount.setName(webAccountVo.getName());
        webAccount.setBaseUrl(webAccountVo.getBaseUrl());
        webAccount.setToken(webAccountVo.getToken());
        webAccount.setUrl(webAccountVo.getUrl());
        webAccount.setAppId(webAccountVo.getAppId());
        webAccount.setAppSecret(webAccountVo.getAppSecret());
        webAccount.setAccessToken(webAccountVo.getAccessToken());
        webAccount.setAccessTokenEndTime(webAccountVo.getAccessTokenEndTime());
        webAccount.setExpiresIn(webAccountVo.getExpiresIn());
        webAccount.setJsTicket(webAccountVo.getJsTicket());
        webAccount.setJsTicketEndTime(webAccountVo.getJsTicketEndTime());
        webAccount.setPayKey(webAccountVo.getPayKey());
        webAccount.setTemplateId(webAccountVo.getTemplateId());
        webAccount.setPayMchId(webAccountVo.getPayMchId());
        webAccount.setXcxAppId(webAccountVo.getXcxAppId());
        webAccount.setXcxAppSecret(webAccountVo.getXcxAppSecret());
        webAccount.setXcxAccessToken(webAccountVo.getXcxAccessToken());
        webAccount.setXcxAccessTokenEndTime(webAccountVo.getXcxAccessTokenEndTime());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(webAccount);
        }else{
            num=insert(webAccount);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-4-26 11:34:01
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        WebAccount webAccount=selectByPrimaryKey(id);
        if(webAccount!=null){
            webAccount.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            webAccount.setUpdateTime(new Date());
            webAccount.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(webAccount);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param webAccountVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-4-26 11:34:01
     */
    public List<WebAccount> acceptList(WebAccountVo webAccountVo){
        Example example = new Example(WebAccount.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(webAccountVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",webAccountVo.getRemoveTag());
        }
        example.setOrderByClause(" update_time desc");
        return webAccountDao.selectByExample(example);
    }
}
